/**
 * 
 */
package com.gsit.misn.bilders;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gsit.misn.domain.Misn;
import com.gsit.misn.domain.Patient;
import com.gsit.misn.domain.User;
import com.gsit.misn.utils.ExcelWriter;
import com.gsit.misn.utils.Writer;
import com.thoughtworks.selenium.DefaultSelenium;

/**
 * @author udichee
 *
 */
public class SnapTestPatientBilder implements PatientBilder<Misn> {
	
	private static Logger logger = LoggerFactory
			.getLogger(LoadPatientBilder.class);

	private List<Patient> patients ;
	private final Integer noOfRecordPerPage = 10;
	private Integer currNoOfRecords = 0;
	private String searchURL;
	private Integer rowNo;
	private Writer<User, List<Patient>> writer;
	private Misn misn;
	
	public SnapTestPatientBilder(){
		patients = new ArrayList<Patient>();
		writer = new ExcelWriter();
	}

	@Override
	public void excute(DefaultSelenium selenium, final Misn misn) {

		this.misn = misn;
		Integer totalLoops = misn.getUser().getTotalNoOfPages()/noOfRecordPerPage;
		//Integer totalLoops = 1;
		Integer startPage =0;
		currNoOfRecords= startPage*10;
		
		for (int page = startPage; page < totalLoops+1; page++) {
			this.pageNavigation(selenium,page);
		}
		
		this.creatExcelForPatient();
	}

	private void pageNavigation(DefaultSelenium selenium, int pageNo) {

		try {
			
			searchURL = misn.getMisnLinks().getSearchUrl() + "?pageNum_r="+ pageNo + "&totalRows_r="+ misn.getUser().getTotalNoOfPages();
			selenium.open(searchURL);
			selenium.waitForPageToLoad("10000");
		
			for (int row = 2; row < noOfRecordPerPage + 2; row++) {
				
				currNoOfRecords++;
				System.out.println("The curr :: "+currNoOfRecords + "  :: Total no of pages :: "+misn.getUser().getTotalNoOfPages());
				if (currNoOfRecords.equals(misn.getUser().getTotalNoOfPages()+1)) {

					System.out.println("The record is finished ...");
					break;
				} else {
					System.out.println("Record no [ " + currNoOfRecords	+ " ]  is processing ........");
				}
					rowNo = (pageNo * noOfRecordPerPage) + row - 2;
				
				
				createPatient(selenium,row);
				
			}
		} catch (Exception e) {
			logger.error("Error is in selenium object retriving...");
			e.printStackTrace();
		}

	}

	private void createPatient(DefaultSelenium selenium,int row) {
		try {
			Patient patient= new Patient();
			System.out.println(selenium.getText("//table//tr[" + row + "]/td[1]"));
			System.out.println(selenium.getText( "//table//tr[" + row + "]/td[2]"));
			
			patient.setPatientID(selenium.getText("//table//tr[" + row + "]/td[1]"));
			patient.setPatientName(selenium.getText( "//table//tr[" + row + "]/td[2]"));
			patient.setRowNo(rowNo);
			patients.add(patient);
			
		} catch (Exception e) {
			logger.error("Error is in selenium object processing");
			e.printStackTrace();
		}

	}

	private void creatExcelForPatient() {
		try {
			writer.write(misn.getUser(), patients);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
